package echat.future.cn.echat.my.view;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityBlacklistBinding;
import echat.future.cn.echat.databinding.ActivityCollegeBinding;
import echat.future.cn.echat.home.AnchorHomeActivity;
import echat.future.cn.echat.my.adapter.CollegeAdapter;
import echat.future.cn.echat.my.adapter.LikeAdapter;
import echat.future.cn.echat.my.adapter.SpacesItemDecoration;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.CollegeBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * 关注的
 */
public class LikeActivity extends BaseActivity<ActivityBlacklistBinding>{


    /**
     * 适配器
     */
    BaseQuickAdapter collegeAdapter;

    /**
     * 导航栏
     */
    AdapterNavagation navagation;


    private List<CollegeBean> colleges = new ArrayList<>();

    @Override
    public int layoutId() {
        return R.layout.activity_blacklist;
    }

    @Override
    public IBaseNavagation navagation() {
        navagation =  new AdapterNavagation(this).
                setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);

        return navagation;
    }

    @Override
    public void initView() {
        navagation.setTitle("我关注的", 16, R.color.white);
        collegeAdapter = new LikeAdapter(colleges);
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        bind.recyclerView.setAdapter(collegeAdapter);
        bind.recyclerView.addItemDecoration(new SpacesItemDecoration(this,colleges));
        load(1);
        collegeAdapter.setOnLoadMoreListener(() -> {
            page = page+1;
            load(page);
        },bind.recyclerView);
        collegeAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.delete){
                    remove(colleges.get(position).getAnchor_id());
                }
            }
        });
        collegeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(LikeActivity.this, AnchorHomeActivity.class).putExtra("extra_str_id", colleges.get(position).getAnchor_id()+""));

            }
        });
    }

    private int page = 1;

    private void load(int page){
        if(page == 1){
            this.colleges.clear();
            collegeAdapter.notifyDataSetChanged();
        }
        new HttpUtilsRequst<CollegeBean>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getCollege(UserHelper.getToken(),page+""))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<CollegeBean>>) o -> {
                    collegeAdapter.loadMoreComplete();
                    colleges.addAll(o);
                    collegeAdapter.notifyDataSetChanged();
                    if(o.size()<10){
                        collegeAdapter.loadMoreEnd();
                    }
                })
                .start();
    }



    private void remove(int id){
        new HttpUtilsRequst<String>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).openFanse(UserHelper.getToken(),String.valueOf(id),"3"))
                .setmOnRequstSuccessListener(o -> {
                    toast("取消关注成功");
                    page = 1;
                    load(page);
                })
                .start();
    }
}
